package com.example.main;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.example.main.inter.MyCacheNameFactory;
import com.example.main.utils.Base64ImageUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;

import me.shouheng.compress.Compress;
import me.shouheng.compress.RequestBuilder;
import me.shouheng.compress.strategy.Strategies;
import me.shouheng.compress.strategy.config.ScaleMode;

public class CompressImageActivity extends Activity {

    private static final String TAG = "CompressImageActivity";

    private Button btn_ok;
    private ImageView iv_image;

    private static final String src = Environment.getExternalStorageDirectory() + "/aaa/dfs.jpeg";
    private static final String dest = Environment.getExternalStorageDirectory() + "/aaa/";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.compress);
        btn_ok = findViewById(R.id.btn_ok);
        iv_image = findViewById(R.id.iv_image);
        btn_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Compress.with(CompressImageActivity.this, new File(src))
                        .setTargetDir(dest)
                        .setCacheNameFactory(new MyCacheNameFactory("222"))
                        .strategy(Strategies.compressor())
                        .setScaleMode(ScaleMode.SCALE_LARGER)
                        .setConfig(Bitmap.Config.RGB_565)
                        .setMaxWidth(200)
                        .setCompressListener(new RequestBuilder.Callback<File>() {
                            @Override
                            public void onStart() {
                                Log.i(TAG, "onStart: ");
                            }

                            @Override
                            public void onSuccess(File result) {
                                String base64Value = Base64ImageUtils.bitmapToBase64(BitmapFactory.decodeFile(result.getPath()));
                                Log.i(TAG, "onSuccess: " + base64Value);

                                try {
                                    JSONObject jsonObject = new JSONObject();
                                    jsonObject.put("content", base64Value);
                                    iv_image.setImageBitmap(Base64ImageUtils.base64ToBitmap(jsonObject.getString("content")));
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }

                            @Override
                            public void onError(Throwable throwable) {
                                Log.i(TAG, "onError: ");
                            }
                        })
                        .launch();
            }
        });
    }
}
